import request from '@/plugin/axios'
import util from '@/libs/util.js'

export default {

	list (data) {
		return request({
			url: 'mchSysNode/list',
			headers: {},
			method: 'post',
			data,
		});
	},

	async tree (data) {

		var re = await this.list(data);
		
		re.tree = this.buildTree(re.list);
		console.log("所有数据",re)
		return re;
	},

	buildTree (list) {

		var group = {};

		list.forEach(item => {
			if (!group.hasOwnProperty(item.parentId)) {
				group[item.parentId] = [];
			}
			group[item.parentId].push(item);
		});

		list.forEach(item => {
			if (group.hasOwnProperty(item.id)) {
				item.children = group[item.id];
			}
		});

		return group[""];
	},

	async getNodeOptions (data = {}) {
		var re = await this.list(data);

		var list = [];
		re.list.forEach(item => {
			list.push({id: item.id, label: item.title, value: item.id, parentId: item.parentId});
		});

		var ret = {};

		ret.tree = this.buildTree(list);

		return ret;
	},

	info (data) {
		return request({
			url: 'mchSysNode/get',
			headers: {},
			method: 'post',
			data,
		});
	},

	add (data) {
		return request({
			url: 'mchSysNode/add',
			headers: {},
			method: 'post',
			data,
		});
	},

	set (data) {
		return request({
			url: 'mchSysNode/edit',
			headers: {},
			method: 'post',
			data,
		});
	},

	del (data) {
		return request({
			url: 'mchSysNode/del',
			headers: {},
			method: 'post',
			data,
		});
	},
};